home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / (A)P / (A)P1.ADF / Cycloids / palette.c < prev    next >
C/C++ Source or Header  |  1987-05-25  |  14KB  |  366 lines

  1. /***********************************************************************
  2. *  palette.c
  3. *     This program is useful for designing color palettes.
  4. * It may be used stand-alone (I would be setting the 'default map',
  5. * if preferences had a complete color map...), or you can use it
  6. * as a subroutine with minor modifications.
  7. *     One of the unique features of this program is that it prints
  8. * the hex value of the selected color, so you use it to generate
  9. * numeric values for use in programs.
  10. *     (c) 1985 by Charlie Heath of Microsmiths, this program is in
  11. * the public domain.  Not to be used commercially without permission
  12. * from MicroSmiths.
  13. ***********************************************************************/
  14.  
  15. #define I_REV   29
  16. #define G_REV   29
  17.  
  18. #define FAST register      /* Register type variable  */
  19.  
  20. #define DPTH 5       /* Raster dimensions */
  21. #define WDTH 320
  22. #define HGHT 200
  23.  
  24. #define BCOL 1          /* Background color for menus, etc   */
  25.  
  26. #define CTSIZ  32       /* In spite of myself, color table size   */
  27.  
  28. /****    Variables Initialized by init_colors()  **********/
  29. struct ColorMap *p_Co;
  30. USHORT *p_ct;           /* Color table as words */
  31. USHORT default_color[CTSIZ];     /* Save a copy of the defaults   */
  32.  
  33.  
  34. init_colors()
  35. {
  36. FAST int i;
  37.  
  38.    p_Co = (struct ColorMap *)GetColorMap(CTSIZ);
  39.    p_ct = (USHORT *)p_Co->ColorTable;
  40.  
  41.    for ( i=0; i<CTSIZ; i++)
  42.       default_color[i] = p_ct[i];
  43.    default_color[0] = 0x05a; default_color[1] = 0xfff;
  44.    default_color[2] = 0x002; default_color[3] = 0xf80;
  45.    default_color[31] = 0xeb0; default_color[4] = 0x00f;
  46.    default_color[5] = 0xf0f; default_color[6] = 0x0ff;
  47.    default_color[7] = 0xfff; default_color[8] = 0x620;
  48.    default_color[9] = 0xe50; default_color[10] = 0x9f1;
  49.    default_color[11] = 0xd22; default_color[12] = 0x55f;
  50.    default_color[13] = 0x92f; default_color[14] = 0x0f8;
  51.    default_color[15] = 0x000;
  52.    set_defmap();
  53. }
  54.  
  55. clear_colors()
  56. {
  57.    FreeColorMap(p_Co);
  58. }
  59.  
  60.  
  61. /****************************************************************
  62. * set_defmap()
  63. *    This routine sets up a 'default' color map.  It assumes
  64. * you have initialized the variable p_ct to point to a color
  65. * table, and that default_color has the colors you want.       */
  66. set_defmap()
  67. {
  68. FAST int i;
  69.  
  70.    for (i=0; i<CTSIZ ; i++)
  71.       *(p_ct+i) = default_color[i];
  72.  
  73.    set_cmap();
  74. }
  75.  
  76.  
  77.  
  78. /****************************************************************
  79. * set_cmap()
  80. *     This routine loads an updated p_ct color table into the
  81. * viewport.  You must have initialized the variables p_ct and
  82. * vp to point at color table and ViewPort                     */
  83. set_cmap()
  84. {
  85.    LoadRGB4(vp,p_ct,CTSIZ);
  86. }
  87.  
  88.  
  89. /***   palette modifier routines follow    ************************/
  90.  
  91. #define PLX 200      /* Palette Window Size  */
  92. #define PLY 132
  93.  
  94. #define PGAD 0             /* You can offset the gadgets here  */
  95. #define PGHI   PGAD+CTSIZ  /* Offset frm color selectors       */
  96.  
  97. struct IntuiText rtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"R",NL};
  98. struct IntuiText gtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"G",NL};
  99. struct IntuiText btxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"B",NL};
  100.  
  101. struct Image    r_img, g_img, b_img;
  102. struct PropInfo r_prop,g_prop,b_prop;
  103.  
  104. struct Gadget blue_gad = {
  105.    NL, 17,112, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  106.    PROPGADGET,(APTR)&b_img, NL,
  107.    &btxt, NL,(APTR)&b_prop, PGHI+3, NL };
  108.  
  109. struct Gadget green_gad = {
  110.    &blue_gad, 17,97, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  111.    PROPGADGET,(APTR)&g_img, NL,
  112.    >xt, NL,(APTR)&g_prop, PGHI+4, NL };
  113.  
  114. struct Gadget red_gad = {
  115.    &green_gad, 17,82, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  116.    PROPGADGET,(APTR)&r_img, NL,
  117.    &rtxt, NL,(APTR)&r_prop, PGHI+5, NL };
  118.  
  119. struct IntuiText oktxt = {3,2,JAM2,8,2,NL,(UBYTE *)" OK ",NL};
  120. struct IntuiText cntxt = {3,2,JAM2,0,2,NL,(UBYTE *)"Cancel",NL};
  121. struct IntuiText retxt = {3,2,JAM2,4,2,NL,(UBYTE *)"Reset",NL};
  122.  
  123. struct IntuiText mstxt={2,2,JAM1,0,0,NL,(UBYTE *)"MicroSmiths Palette",NL};
  124.  
  125. char frog[] = "hex";
  126. struct IntuiText hxtxt = {3,2,JAM2,136,31,NL,(UBYTE *)frog,NL};
  127.  
  128. struct Gadget re_gad = {
  129.    &red_gad, 110,112, 54,11, GADGHCOMP, RELVERIFY,
  130.    BOOLGADGET, NL, NL,
  131.    &retxt, NL,NL, PGHI+2, NL };
  132. struct Gadget cn_gad = {
  133.    &re_gad, 110,97, 54,11, GADGHCOMP, RELVERIFY,
  134.    BOOLGADGET, NL, NL,
  135.    &cntxt, NL,NL, PGHI+1, NL };
  136. struct Gadget ok_gad = {
  137.    &cn_gad, 110,82, 54,11, GADGHCOMP, RELVERIFY,
  138.    BOOLGADGET, NL, NL,
  139.    &oktxt, NL,NL, PGHI, NL };
  140.  
  141. struct Image m3C[CTSIZ] = {
  142.    {0,0,16,16,1, NL ,0,0,NL },   /* Colors   */
  143.    {0,0,16,16,1, NL ,0,1,NL },
  144.    {0,0,16,16,1, NL ,0,2,NL },
  145.    {0,0,16,16,1, NL ,0,3,NL },
  146.    {0,0,16,16,1, NL ,0,4,NL },
  147.    {0,0,16,16,1, NL ,0,5,NL },
  148.    {0,0,16,16,1, NL ,0,6,NL },
  149.    {0,0,16,16,1, NL ,0,7,NL },
  150.    {0,0,16,16,1, NL ,0,8,NL },
  151.    {0,0,16,16,1, NL ,0,9,NL },
  152.    {0,0,16,16,1, NL ,0,10,NL },
  153.    {0,0,16,16,1, NL ,0,11,NL },
  154.    {0,0,16,16,1, NL ,0,12,NL },
  155.    {0,0,16,16,1, NL ,0,13,NL },
  156.    {0,0,16,16,1, NL ,0,14,NL },
  157.    {0,0,16,16,1, NL ,0,15,NL },
  158.    {0,0,16,16,1, NL ,0,16,NL },
  159.    {0,0,16,16,1, NL ,0,17,NL },
  160.    {0,0,16,16,1, NL ,0,18,NL },
  161.    {0,0,16,16,1, NL ,0,19,NL },
  162.    {0,0,16,16,1, NL ,0,20,NL },
  163.    {0,0,16,16,1, NL ,0,21,NL },
  164.    {0,0,16,16,1, NL ,0,22,NL },
  165.    {0,0,16,16,1, NL ,0,23,NL },
  166.    {0,0,16,16,1, NL ,0,24,NL },
  167.    {0,0,16,16,1, NL ,0,25,NL },
  168.    {0,0,16,16,1, NL ,0,26,NL },
  169.    {0,0,16,16,1, NL ,0,27,NL },
  170.    {0,0,16,16,1, NL ,0,28,NL },
  171.    {0,0,16,16,1, NL ,0,29,NL },
  172.    {0,0,16,16,1, NL ,0,30,NL },
  173.    {0,0,16,16,1, NL ,0,31,NL } };
  174.  
  175. struct Gadget palg[CTSIZ] = {
  176.    { &palg[1], 3, 3,  16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  177.       BOOLGADGET,  (APTR)&m3C[0], NL,NL,NL,NL, PGAD+0, NL},
  178.    { &palg[2], 19,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  179.       BOOLGADGET,  (APTR)&m3C[1], NL,NL,NL,NL, PGAD+1, NL},
  180.    { &palg[3], 35,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  181.       BOOLGADGET,  (APTR)&m3C[2], NL,NL,NL,NL, PGAD+2, NL},
  182.    { &palg[4], 51,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  183.       BOOLGADGET,  (APTR)&m3C[3], NL,NL,NL,NL, PGAD+3, NL},
  184.    { &palg[5], 67,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  185.       BOOLGADGET,  (APTR)&m3C[4], NL,NL,NL,NL, PGAD+4, NL},
  186.    { &palg[6], 83,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  187.       BOOLGADGET,  (APTR)&m3C[5], NL,NL,NL,NL, PGAD+5, NL},
  188.    { &palg[7], 99,3, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  189.       BOOLGADGET,  (APTR)&m3C[6], NL,NL,NL,NL, PGAD+6, NL},
  190.    { &palg[8],115,3,  16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  191.       BOOLGADGET,  (APTR)&m3C[7], NL,NL,NL,NL, PGAD+7, NL},
  192.    { &palg[9], 3,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  193.       BOOLGADGET,  (APTR)&m3C[8], NL,NL,NL,NL, PGAD+8, NL},
  194.    { &palg[10],19,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  195.       BOOLGADGET,  (APTR)&m3C[9], NL,NL,NL,NL, PGAD+9, NL},
  196.    { &palg[11],35,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  197.       BOOLGADGET,  (APTR)&m3C[10], NL,NL,NL,NL, PGAD+10, NL},
  198.    { &palg[12],51,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  199.       BOOLGADGET,  (APTR)&m3C[11], NL,NL,NL,NL, PGAD+11, NL},
  200.    { &palg[13],67,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  201.       BOOLGADGET,  (APTR)&m3C[12], NL,NL,NL,NL, PGAD+12, NL},
  202.    { &palg[14],83,19, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  203.       BOOLGADGET,  (APTR)&m3C[13], NL,NL,NL,NL, PGAD+13, NL},
  204.    { &palg[15],99,19,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  205.       BOOLGADGET,  (APTR)&m3C[14], NL,NL,NL,NL, PGAD+14, NL},
  206.    { &palg[16],115,19,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  207.       BOOLGADGET,  (APTR)&m3C[15], NL,NL,NL,NL, PGAD+15, NL},
  208.    { &palg[17], 3,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  209.       BOOLGADGET, (APTR)&m3C[16], NL,NL,NL,NL, PGAD+16, NL},
  210.    { &palg[18],19,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  211.       BOOLGADGET,  (APTR)&m3C[17], NL,NL,NL,NL, PGAD+17, NL},
  212.    { &palg[19],35,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  213.       BOOLGADGET,  (APTR)&m3C[18], NL,NL,NL,NL, PGAD+18, NL},
  214.    { &palg[20],51,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  215.       BOOLGADGET,  (APTR)&m3C[19], NL,NL,NL,NL, PGAD+19, NL},
  216.    { &palg[21],67,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  217.       BOOLGADGET,  (APTR)&m3C[20], NL,NL,NL,NL, PGAD+20, NL},
  218.    { &palg[22],83,35, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  219.       BOOLGADGET,  (APTR)&m3C[21], NL,NL,NL,NL, PGAD+21, NL},
  220.    { &palg[23],99,35,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  221.       BOOLGADGET,  (APTR)&m3C[22], NL,NL,NL,NL, PGAD+22, NL},
  222.    { &palg[24],115,35,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  223.       BOOLGADGET,  (APTR)&m3C[23], NL,NL,NL,NL, PGAD+23, NL},
  224.    { &palg[25], 3,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  225.       BOOLGADGET, (APTR)&m3C[24], NL,NL,NL,NL, PGAD+24, NL},
  226.    { &palg[26],19,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  227.       BOOLGADGET,  (APTR)&m3C[25], NL,NL,NL,NL, PGAD+25, NL},
  228.    { &palg[27],35,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  229.       BOOLGADGET,  (APTR)&m3C[26], NL,NL,NL,NL, PGAD+26, NL},
  230.    { &palg[28],51,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  231.       BOOLGADGET,  (APTR)&m3C[27], NL,NL,NL,NL, PGAD+27, NL},
  232.    { &palg[29],67,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  233.       BOOLGADGET,  (APTR)&m3C[28], NL,NL,NL,NL, PGAD+28, NL},
  234.    { &palg[30],83,51, 16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  235.       BOOLGADGET,  (APTR)&m3C[29], NL,NL,NL,NL, PGAD+29, NL},
  236.    { &palg[31],99,51,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  237.       BOOLGADGET,  (APTR)&m3C[30], NL,NL,NL,NL, PGAD+30, NL},
  238.    { &ok_gad, 115,51,16,16, GADGHBOX | GADGIMAGE, RELVERIFY,
  239.       BOOLGADGET,  (APTR)&m3C[31], NL,NL,NL,NL, PGAD+31, NL} };
  240.  
  241.  
  242. /* Used to open a Window   */
  243. struct NewWindow NewCmod = {
  244.    40,30, PLX,PLY, 2,BCOL, NL,    /* IDCMP set up AFTER CALL */
  245.    ACTIVATE | SMART_REFRESH,
  246.    &palg[0],NL, NL,    /* FirstGadget, CheckMark, Title  */
  247.    NL,NL,              /* MUST SET SCREEN AFTER OPENSCREEN!!! */
  248.    PLX,PLY,PLX,PLY, CUSTOMSCREEN }; /* MinW, MinH, MaxW, MaxH */
  249.  
  250.  
  251. /********************************************************************
  252. * palette(window)
  253. *    This is the meat. This routine opens the palette window and
  254. * retains control until the user selects the OK or CANCEL gadget.
  255. *     The calling arguement is a window pointer.  That window
  256. * is expected to have opened an IDCMP port, which palette uses.
  257. ********************************************************************/
  258. palette(calling_window)
  259. struct Window *calling_window;
  260. {
  261. struct Window *cW;      /* Palette window handle   */
  262.  
  263. FAST struct IntuiMessage *imsg;
  264. FAST struct Gadget *igad;
  265.  
  266. FAST int class,cursel;
  267. BOOL keepon,munge;
  268.  
  269. USHORT backup[CTSIZ];         /* This table restores calling colors... */
  270.  
  271. static char hxtab[16] = { '0','1','2','3','4','5','6','7','8',
  272.    '9','a','b','c','d','e','f' };
  273.  
  274.    for ( cursel=CTSIZ-1; cursel >= 0; cursel--)
  275.       backup[cursel] = p_ct[cursel];
  276.  
  277.    r_prop.Flags = g_prop.Flags = b_prop.Flags = FREEHORIZ | AUTOKNOB;
  278.    r_prop.HorizBody = g_prop.HorizBody = b_prop.HorizBody = 0x1000;
  279.  
  280.    /* Get screen from calling window   */
  281.    NewCmod.Screen = calling_window->WScreen;  /* NEED TO SET SCREEN!!! */
  282.    if ( ! (cW = (struct Window *)OpenWindow(&NewCmod)) ) {
  283.       return(FALSE); /* Oops...  */
  284.       }
  285.  
  286.    PrintIText(cW->RPort,&mstxt,7,71);
  287.  
  288.    cW->UserPort = calling_window->UserPort;
  289.    ModifyIDCMP(cW, GADGETUP | GADGETDOWN | MENUVERIFY );
  290.  
  291.    for ( munge = keepon = TRUE; keepon; ) {
  292.  
  293.       if ( munge ) {
  294.          SetAPen(cW->RPort,cursel);
  295.          RectFill(cW->RPort,133,3,PLX-5,66);
  296.  
  297.          /* RJ will proably cringe if he see's this, but it is proably */
  298.          /* safe to modify the HorizPot values this way, UNLESS the    */
  299.          /* gadgets were being fiddled with when you do it. Here that  */
  300.          /* is quite unlikely, since another gadget was just activated */
  301.          /* to cause the munge flag to be set...                       */
  302.  
  303.          r_prop.HorizPot = (p_ct[cursel] & 0xf00) << 4;
  304.          g_prop.HorizPot = (p_ct[cursel] & 0x0f0) << 8;
  305.          b_prop.HorizPot = p_ct[cursel] << 12;
  306.          RefreshGadgets(&red_gad,cW,NL);
  307.          munge = FALSE;
  308.          hxtxt.FrontPen = cursel ^ 15;
  309.          hxtxt.BackPen  = cursel;
  310.          }
  311.  
  312.       while ( ! (imsg=(struct IntuiMessage *)GetMsg(cW->UserPort)) ) {
  313.          class = p_ct[cursel] = ((r_prop.HorizPot >> 4) & 0xf00) +
  314.             ((g_prop.HorizPot >> 8) & 0xf0) + (b_prop.HorizPot >>12);
  315.          set_cmap();
  316.  
  317.          frog[0] = hxtab[class >> 8];
  318.          frog[1] = hxtab[ (class >> 4) & 0x0f];
  319.          frog[2] = hxtab[class & 0x0f];
  320.          PrintIText(cW->RPort,&hxtxt,0,0);
  321.  
  322.          /* Proably, should do a WaitPort(cW->UserPort); */
  323.          /* I didn't, so the color updates faster.       */
  324.          /* In a multitasking environment, the system is */
  325.          /* being gronked...                             */
  326.          }
  327.  
  328.       igad =(struct Gadget *) imsg->IAddress;
  329.       if ( (class = imsg->Class) == MENUVERIFY ) {
  330.          imsg->Code = MENUCANCEL;      /* Don't Let Em Out! */
  331.          DisplayBeep(NL);
  332.          }
  333.       ReplyMsg(imsg);
  334.  
  335.       switch ( class ) {
  336.          case GADGETUP:
  337.          case GADGETDOWN:
  338.             if ((igad->GadgetID >= PGAD)&&(igad->GadgetID < PGAD+CTSIZ)) {
  339.                cursel = igad->GadgetID - PGAD;
  340.                munge = TRUE;
  341.                }
  342.             else switch ( igad->GadgetID ) {
  343.                case PGHI+1:      /* Cancel   */
  344.                   for ( class =0 ; class<CTSIZ; class++)
  345.                      p_ct[class] = backup[class];
  346.                   set_cmap();
  347.                case PGHI:        /* OK */
  348.                   keepon = FALSE;
  349.                   break;
  350.  
  351.                case PGHI+2:      /* Reset */
  352.                   set_defmap();
  353.                   munge = TRUE;
  354.                   break;
  355.                }
  356.          }
  357.       }
  358.  
  359.    cW->UserPort = NL;
  360.    CloseWindow(cW);
  361.    return(TRUE);
  362. }
  363.  
  364.  
  365. /*************************************************************/
  366.